package org.zebra.mybatis.plus.ext;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghongbin
 */
@Slf4j
public class MetaObjectHandlerPlus implements MetaObjectHandler {

    private List<MetaObjectHandler> handlers = new ArrayList<>();

    public void addMetaObjectHandler(MetaObjectHandler metaObjectHandler) {
        this.handlers.add(metaObjectHandler);
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        handlers.forEach(m -> {
            try {
                m.insertFill(metaObject);
            } catch (Exception e) {
                log.error(m.getClass().getName(), e);
            }
        });
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        handlers.forEach(m -> {
            try {
                m.updateFill(metaObject);
            } catch (Exception e) {
                log.error(m.getClass().getName(), e);
            }
        });
    }
}
